"""
人物相关API接口
"""
from typing import List, Optional, Dict, Any
from fastapi import APIRouter, Depends, Query
from sqlalchemy.orm import Session

from app.core.database import get_db
from app.api.dependencies import get_character_service
from app.services.character_service import CharacterService
from app.models.character import RoleType, Gender
from app.models.schemas.character import (
    CharacterCreate, CharacterUpdate, CharacterResponse,
    CharacterBasicInfoUpdate, PersonalityTagRequest
)

router = APIRouter()

@router.get("/", response_model=List[CharacterResponse])
async def get_all_characters(
    skip: int = Query(0, ge=0, description="跳过记录数"),
    limit: int = Query(100, ge=1, le=1000, description="限制记录数"),
    character_service: CharacterService = Depends(get_character_service)
):
    """获取所有人物"""
    return character_service.get_all_characters(skip=skip, limit=limit)

@router.get("/{character_id}", response_model=CharacterResponse)
async def get_character(
    character_id: int,
    character_service: CharacterService = Depends(get_character_service)
):
    """获取人物详情"""
    return character_service.get_character(character_id)

@router.post("/", response_model=CharacterResponse)
async def create_character(
    character_data: CharacterCreate,
    character_service: CharacterService = Depends(get_character_service)
):
    """创建人物"""
    return character_service.create_character(character_data.dict())

@router.put("/{character_id}", response_model=CharacterResponse)
async def update_character(
    character_id: int,
    character_data: CharacterUpdate,
    character_service: CharacterService = Depends(get_character_service)
):
    """更新人物"""
    update_data = character_data.dict(exclude_unset=True)
    return character_service.update_character(character_id, update_data)

@router.delete("/{character_id}")
async def delete_character(
    character_id: int,
    character_service: CharacterService = Depends(get_character_service)
):
    """删除人物"""
    success = character_service.delete_character(character_id)
    return {"success": success, "message": "Character deleted successfully"}

@router.get("/search/", response_model=List[CharacterResponse])
async def search_characters(
    keyword: str = Query(..., min_length=1, description="搜索关键词"),
    character_service: CharacterService = Depends(get_character_service)
):
    """搜索人物"""
    return character_service.search_characters(keyword)

@router.get("/role/{role_type}", response_model=List[CharacterResponse])
async def get_characters_by_role(
    role_type: RoleType,
    character_service: CharacterService = Depends(get_character_service)
):
    """根据角色类型获取人物"""
    return character_service.get_characters_by_role(role_type.value)

@router.get("/department/{department}", response_model=List[CharacterResponse])
async def get_characters_by_department(
    department: str,
    character_service: CharacterService = Depends(get_character_service)
):
    """根据部门获取人物"""
    return character_service.get_characters_by_department(department)

@router.get("/protagonist/", response_model=CharacterResponse)
async def get_protagonist(
    character_service: CharacterService = Depends(get_character_service)
):
    """获取主角"""
    protagonist = character_service.get_protagonist()
    if not protagonist:
        return {"message": "No protagonist found"}
    return protagonist

@router.get("/colleagues/", response_model=List[CharacterResponse])
async def get_colleagues(
    character_service: CharacterService = Depends(get_character_service)
):
    """获取所有同事"""
    return character_service.get_colleagues()

@router.get("/superiors/", response_model=List[CharacterResponse])
async def get_superiors(
    character_service: CharacterService = Depends(get_character_service)
):
    """获取所有上级"""
    return character_service.get_superiors()

@router.get("/age-range/", response_model=List[CharacterResponse])
async def get_characters_by_age_range(
    min_age: int = Query(..., ge=0, description="最小年龄"),
    max_age: int = Query(..., ge=0, description="最大年龄"),
    character_service: CharacterService = Depends(get_character_service)
):
    """根据年龄范围获取人物"""
    return character_service.get_characters_by_age_range(min_age, max_age)

@router.get("/gender/{gender}", response_model=List[CharacterResponse])
async def get_characters_by_gender(
    gender: Gender,
    character_service: CharacterService = Depends(get_character_service)
):
    """根据性别获取人物"""
    return character_service.get_characters_by_gender(gender.value)

@router.patch("/{character_id}/basic-info", response_model=CharacterResponse)
async def update_character_basic_info(
    character_id: int,
    basic_info_data: CharacterBasicInfoUpdate,
    character_service: CharacterService = Depends(get_character_service)
):
    """更新人物基本信息"""
    return character_service.update_character_basic_info(
        character_id, 
        basic_info_data.basic_info
    )

@router.post("/{character_id}/personality-tags", response_model=CharacterResponse)
async def add_personality_tag(
    character_id: int,
    tag_request: PersonalityTagRequest,
    character_service: CharacterService = Depends(get_character_service)
):
    """添加性格标签"""
    return character_service.add_personality_tag(
        character_id, 
        tag_request.tag
    )

@router.delete("/{character_id}/personality-tags", response_model=CharacterResponse)
async def remove_personality_tag(
    character_id: int,
    tag_request: PersonalityTagRequest,
    character_service: CharacterService = Depends(get_character_service)
):
    """移除性格标签"""
    return character_service.remove_personality_tag(
        character_id, 
        tag_request.tag
    )

@router.get("/statistics/summary")
async def get_character_statistics(
    character_service: CharacterService = Depends(get_character_service)
):
    """获取人物统计信息"""
    return character_service.get_character_statistics()